<template>
  <el-container direction="vertical">

    <navMenu width="100%"></navMenu>

    <el-container direction="horizontal">
      <el-aside width="280px" height="100%">
        <sideBar class="menu" page-index="5-1" open-index="5" ></sideBar>
      </el-aside>
      <el-main>
        <!--搜索栏-->
        <el-container direction="horizontal">
          <el-input v-model="words.word" placeholder="请输入风险名称 （支持模糊查询）"></el-input>
          <el-button style="margin-left: 20px" @click="vague()">搜索</el-button>

          <!--新建抽屉-->
          <el-drawer
            title="新增风险点处置"
            :visible.sync="drawer"
            :direction="direction"
            ref="thisdrawer">

            <el-row :gutter="20">
              <el-col :span="6" :offset="2">选择风险点</el-col>
            </el-row>

            <!--风险点选择框-->
            <el-row :gutter="20" style="margin-top: 12px">
              <el-col :span="20" :offset="2">
                <template>
                  <el-select v-model="newCopeRecord.risk_id" clearable placeholder="请选择">
                    <el-option
                      v-for="item in options"
                      :key="item.risk_id"
                      :label="item.risk_id + ' ' + item.name"
                      :value="item.risk_id">
                    </el-option>
                  </el-select>
                </template>
              </el-col>
            </el-row>

            <el-row :gutter="20" style="margin-top: 40px">
              <el-col :span="6" :offset="2">
                <el-button @click="addCopeRecord()" type="primary" style="width: 130%">确认提交</el-button></el-col>
            </el-row>
          </el-drawer>
        </el-container>

        <!--风险点处置列表-->
        <el-table
          :data="tableData.slice((currentPage-1)*pagesize,currentPage*pagesize)"
          height="502"
          style="width: 100%; margin-top: 20px">
          <el-table-column type="expand">
            <!--表格行扩展，显示风险处置情况-->
            <template slot-scope="scope">
              <el-steps v-if="scope.row.status < 3" :active="scope.row.status" finish-status="success">
                <el-step v-if="scope.row.status===0" title="正在处置考评"></el-step>
                <el-step v-if="scope.row.status===1 || scope.row.status===2" title="处置考评完成"></el-step>
                <el-step v-if="scope.row.status===0" title="未处置结果分析"></el-step>
                <el-step v-if="scope.row.status===1" title="正在处置结果分析"></el-step>
                <el-step v-if="scope.row.status===2" title="处置结果分析完成"></el-step>
              </el-steps>

              <el-steps v-if="scope.row.status >= 3" :active="scope.row.status-2" finish-status="success">
                <el-step v-if="scope.row.status===3 " title="处置考评失败" status="error"></el-step>
                <el-step v-if="scope.row.status===4 " title="处置考评完成"></el-step>
                <el-step v-if="scope.row.status===3" title="未处置结果分析"></el-step>
                <el-step v-if="scope.row.status===4" title="处置结果分析失败" status="error"></el-step>
              </el-steps>
            </template>
          </el-table-column>

          <el-table-column
            prop="risk_cope_id"
            label="处置编号">
          </el-table-column>

          <el-table-column
            prop="time"
            label="处置时间" >
          </el-table-column>

          <el-table-column
            prop="risk_id"
            label="风险点编号">
          </el-table-column>

          <el-table-column
            prop="name"
            label="风险点名称">
          </el-table-column>

          <!--风险的处置状态，带颜色的文字链接-->
          <el-table-column
            prop="status"
            label="处置状态">
            <template slot-scope="scope">
              <el-link v-if='scope.row.status===0' type="primary" :underline="false">
                正在处置考评
              </el-link>
              <el-link v-if='scope.row.status===1' type="primary" :underline="false">
                正在处置结果分析
              </el-link>
              <el-link v-if='scope.row.status===2' type="success" :underline="false">
                处置完成
              </el-link>
              <el-link v-if='scope.row.status===3' type="danger" :underline="false">
                处置考评失败
              </el-link>
              <el-link v-if='scope.row.status===4' type="danger" :underline="false">
                处置结果分析失败
              </el-link>
            </template>>
          </el-table-column>

          <el-table-column
            label="操作">
            <template slot-scope="scope">
              <el-button
                size="small"
                @click="showDetail(scope.row)">
                更多信息
                <i class="el-icon-s-order"></i>
              </el-button>
              <el-button icon="el-icon-delete" circle size="small"
                         @click="handleDelete(scope.$index, scope.row)">
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <!--角标-->
        <div style="text-align: center;margin-top: 20px;">
          <el-pagination
            background
            layout="prev, pager, next"
            :total="total"
            :current-page="pagesize"
            @current-change="current_change">
          </el-pagination>
        </div>
      </el-main>

    </el-container>
  </el-container>
</template>

<script>
export default {
  name: "copeRecord",
  data() {
    return {
      words: {
        word: ''
      },
      total: 0,
      drawer: false,
      direction: 'rtl',
      tableData: [],
      currentPage: 1,
      pagesize: 8,
      active: 2,
      error: 0,
      newCopeRecord:{
        risk_id:''
      },
      options: [{
        risk_id: '',
        name:''
      }],

    }
  },mounted() {
    //初始化表格
    this.$axios.get(`http://127.0.0.1:5000/riskCopeRecord/init`).then((res) => {
      this.tableData = res.data;
      this.total = this.tableData.length;
      console.log("init")
    });
    //初始化抽屉的下拉选择框
    this.$axios.get(`http://127.0.0.1:5000/riskCopeRecord/getRiskForCope`).then((res) => {
      console.log("getRiskForCope")
      console.log(res);
      this.options = res.data
    });
  },
  methods:{
    current_change: function(currentPage){
      this.currentPage = currentPage;
    }, async vague() {
      this.$axios.post(`http://127.0.0.1:5000/riskCopeRecord/select`, this.words).then((res) => {
        console.log(res);
        if(res.data===0){
          this.$message('没有符合查询条件的结果');
        }
        else {
          this.tableData = res.data;
          this.total = this.tableData.length;
        }
      });
  }, addCopeRecord: function(){
      if (this.newCopeRecord.risk_id === '')
        this.$message('请选择风险点');
      else
        this.$axios.post(`http://127.0.0.1:5000/riskCopeRecord/addRiskCope`, this.newCopeRecord).then((res) => {
          console.log(res);
          this.st = res.data;

          if (this.st===1) {
            this.$message('新增成功');
            this.$refs.thisdrawer.closeDrawer();
            window.location.reload();
          }
          else {
            this.$message('新增失败，风险点处置已存在！');
            this.$refs.thisdrawer.closeDrawer();
          }
          this.st=2;

        });
    }, async handleDelete($index, row) {
      this.$axios.post(`http://127.0.0.1:5000/riskCopeRecord/delete`, row.risk_cope_id).then((res) => {
        console.log(res);
        this.st = res.data;
        if (this.st===1){
          this.$message('删除成功');
          window.location.reload();}
        else{
          this.$message('删除失败');
        }

        this.st = 2;
      });
    },showDetail(row) {
      this.$router.push({name:'riskCope-detail',query:{risk_cope_id: row.risk_cope_id}})
    },

  }

}
</script>

<style scoped>
.menu{
  width: 100%;
  float: left;
  height: 673px;
}
</style>
